package com.changgou.system.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/*
* JWT校验工具
* */
public class JwtUtil {
    //有效期为
    public static final Long JWT_TTL = 3600000L; //一个小时
    //设置密钥明文
    public static final String JWT_KEY = "itcast";

    /**
     * 生成加密过后的密钥 secretKey
     * @return
     */
    public static SecretKey generaKey() {
        byte[] encodedKey = Base64.getDecoder().decode( JwtUtil.JWT_KEY );
        SecretKey key = new SecretKeySpec( encodedKey,0,encodedKey.length,"AES" );
        return key;
    }

    /**
     * 解析
     * @param jwt
     * @return
     * @throws Exception
     */

    public static Claims parseJWT(String jwt) throws Exception{
        SecretKey secretKey = generaKey();
        return Jwts.parser()
                .setSigningKey( secretKey )
                .parseClaimsJws( jwt )
                .getBody();
    }

}
